function [ accuracy ] = crossvalidate( xtrainData, ytrainData, lambda )
accuracy = 0;



for n=1:5
    count = 0;
    %partition xtrain and ytrain
    if(n==1)
        xtest = xtrainData(1:613,:);
        xtrain = xtrainData(614:3065,:);
        ytest = ytrainData(1:613,:);
        ytrain = ytrainData(614:3065,:);
    elseif(n==2)
        xtest = xtrainData(614:1226,:);
        xtrain = [xtrainData(1:613,:);xtrainData(1227:3065,:)];
        ytest = ytrainData(614:1226,:);
        ytrain = [ytrainData(1:613,:);ytrainData(1227:3065,:)];
    elseif(n==3)
        xtest = xtrainData(1227:1839,:);
        xtrain = [xtrainData(1:1226,:);xtrainData(1840:3065,:)];
        ytest = ytrainData(1227:1839,:);
        ytrain = [ytrainData(1:1226,:);ytrainData(1840:3065,:)]; 
    elseif(n==4)
        xtest = xtrainData(1840:2452,:);
        xtrain = [xtrainData(1:1839,:);xtrainData(2453:3065,:)];
        ytest = ytrainData(1840:2452,:);
        ytrain = [ytrainData(1:1839,:);ytrainData(2453:3065,:)];
    elseif(n==5)
        xtest = xtrainData(2453:3065,:);
        xtrain = xtrainData(1:2452,:);
        ytest = ytrainData(2453:3065,:);
        ytrain = ytrainData(1:2452,:);   
    end
    
    
    weights=logregnew(xtrain,ytrain,lambda);
    testProb = zeros(size(xtest,1),1);
    for testIndex=1:size(xtest,1)
        testProb(testIndex) = 1/(1+exp(-weights' * xtest(testIndex,:)'));
    end
    testResult = round(testProb);
    testError = sum(testResult ~= ytest)/size(ytest,1);
    
    accuracy = accuracy + testError;
    
end
accuracy = accuracy/5;

end

